<?php
include_once('Sqlexecutor.php');
class Login
{
    private $_dbh;
    private $_logger;

    public function __construct($dbh, $logger = Null)
    {/*{{{*/
        $this->_dbh = $dbh; 
        $this->_logger = $logger; 
    }/*}}}*/

    public function getInfoByID($userid)
    {/*{{{*/
        $sql = "select `email`, `username`, `password`, `name`, `tel`, `corp_name`, `address`, `status` from bs_user where id = ?";
        $val[] = $userid;
        try
        {
            $info = $this->_dbh->query($sql, $val);
            if($info === false)
                $info = NULL;
            return $info;
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return false;
        }
    }/*}}}*/

    public function getList()
    {/*{{{*/
        $sql = "select `id`, `email`, `username`, `password`, `name`, `tel`, `corp_name`, `address`, `status`, `insert_date`, `update_date` from bs_user order by `status`";
        try
        {
            $list = $this->_dbh->querys($sql);
            return $list;
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return false;
        }
    }/*}}}*/

    public function create($email, $username, $password, $name, $tel, $corp_name, $address, $status = 0)
    {/*{{{*/
        $sql = "insert into bs_user set `email` = ?, `username` = ?, `password` = ?, `name` = ?, `tel` = ?, `corp_name` = ?, `address` = ?, `status` = ?, `insert_date` = now()";
        $val[] = $email;
        $val[] = $username;
        $val[] = md5($password);
        $val[] = $name;
        $val[] = $tel;
        $val[] = $corp_name;
        $val[] = $address;
        $val[] = $status;
        try
        {
            $this->_dbh->beginTrans();
            $this->_dbh->execute($sql, $val); 
            $lastId = $this->_dbh->getLastInsertID();
            $this->_dbh->commit();

            return $lastId;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/ 

    public function updateByID($email, $name, $tel, $corp_name, $address, $id)
    {/*{{{*/
        $sql = 'update bs_user set `email` = ?, `name` = ?, `tel` = ?, `corp_name` = ?, `address` = ? where `id` = ?';
        $val[] = $email;
        $val[] = $name;
        $val[] = $tel;
        $val[] = $corp_name;
        $val[] = $address;
        $val[] = $id;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/

    public function adminResetPassByID($password, $id)
    {/*{{{*/
        $sql = 'update bs_user set `password` = ? where `id` = ?';
        $val[] = md5($password);
        $val[] = $id;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());

            // Unknow error
            return -100;
        }
    }/*}}}*/

    public function updatePassByID($oldpassword, $password, $usrid)
    {/*{{{*/
        $sql = "select `password` from bs_user where id = ?";
        $val[] = $usrid;
        $info = array();
        try
        {
            $info = $this->_dbh->query($sql, $val);
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return -100;
        }

        $pass = isset($info['password']) ? $info['password'] : '';
        if(md5($oldpassword) != $pass)
        {
            return -3;
        }

        $sql = 'update bs_user set `password` = ? where `id` = ?';
        $val = array();
        $val[] = md5($password);
        $val[] = $usrid;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            if($e->errorInfo[1] == 1062 && $e->errorInfo[0] == "23000")
            {
                // Duplicate key
                return -1;
            }
            else
            {
                // Unknow error
                return -100;
            }
        }
    }/*}}}*/

    public function confirmByID($id)
    {/*{{{*/
        $sql = 'update bs_user set `status` = 1 where `id` = ?';
        $val[] = $id;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not change, 1 : change 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            // Unknow error
            return -100;
        }
    }/*}}}*/

    public function deleteByID($id)
    {/*{{{*/
        $sql = 'delete from bs_user where `id` = ?';
        $val[] = $id;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
            
            // 0: not delete, 1 : delete 1 rows
            return $res;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            
            // Unknow error
            return -100;
        }
    }/*}}}*/

    public function login($username, $password)
    {/*{{{*/
        $sql = "select `id`, `status` from bs_user where `username` = ? and `password` = ?";
        $val[] = $username;
        $val[] = md5($password);
        $info = array();
        try
        {
            $info = $this->_dbh->query($sql, $val);
            if($info === false)
            {
                return -1;
            }
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return -100;
        }
        $sql = 'delete from bs_session where `usrid` = ?';
        $val = array();
        $val[] = $info['id'];
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            // Unknow error
            return -100;
        }

        $sql = 'insert into bs_session set `usrid` = ?, `sessionid` = ?, `status` = ?, `insert_date` = now()';
        $val = array();
        $val[] = $info['id'];
        $sessionid = uniqid();
        $val[] = $sessionid;
        $val[] = $info['status'];
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            // Unknow error
            return -100;
        }

        $res = array();
        $res['usrid'] = $info['id'];
        $res['sessionid'] = $sessionid;
        $res['status'] = $info['status'];
        return $res;
    }/*}}}*/

    public function logout($usrid)
    {/*{{{*/
        $sql = 'delete from bs_session where `usrid` = ?';
        $val = array();
        $val[] = $usrid;
        try
        {
            $this->_dbh->beginTrans();
            $res = $this->_dbh->execute($sql, $val); 
            $this->_dbh->commit();

            return 0;
        }
        catch(Exception $e)
        {
            $this->_dbh->rollback();
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            // Unknow error
            return -100;
        }
    }/*}}}*/

    public function isLogin($usrid, $sessionid)
    {/*{{{*/
        $sql = 'select `status` from bs_session where `usrid` = ? and `sessionid` = ?';
        $val[] = $usrid;
        $val[] = $sessionid;
        try
        {
            $info = $this->_dbh->query($sql, $val);
            if($info === false)
            {
                return -1;
            }
            else
            {
                return $info['status'];
            }
        }
        catch(Exception $e)
        {
            if($this->_logger)
                $this->_logger->error($e->getFile() . ":" . $e->getLine() . "," . $e->getMessage());
            return -100;
        }

    }/*}}}*/
}

